package com.kfm.ajax.controller;

import com.kfm.ajax.model.Goods;
import com.kfm.ajax.model.Result;
import com.kfm.ajax.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
// 该类中所有请求都可以支持跨域访问
@CrossOrigin("*")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    // redis --> 数据库 持久化
    // ip 登录次数限制 redis ip 1    ACCESS_KEY_ip   BLACK_LIST_ip  1
    // key - value   Map<k,v>


    @GetMapping("/list")
    public Result list() {
        List<Goods> all = goodsService.findAll();
        if (all != null) {
            return Result.success("查询成功", all);
        }
        return Result.failure("查询失败");

    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        int i = goodsService.deleteByPrimaryKey(id);
        return i > 0 ? Result.success("删除成功") : Result.failure("删除失败");
    }

    @PostMapping("/test")
    // 从 http://localhost:63342 发送过来的请求允许通过
    @CrossOrigin("*")
    public Result test( Goods goods) {
        System.out.println(goods.getGoodsName());
        System.out.println(goods.getGoodsPrice());
        return Result.success("成功");
    }
}
